#include <stdio.h>
#include <math.h>
#define M 8
#define N 8
#define MaxSize 100
typedef int ElemType;
int a[M+2][N+2]=
{
{1,1,1,1,1,1,1,1,1,1},
{1,0,1,1,0,0,0,1,0,1},
{1,0,1,1,0,0,0,1,0,1},
{1,0,0,0,0,1,1,0,0,1},
{1,0,1,1,1,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,1},
{1,0,1,0,0,0,1,0,0,1},
{1,0,1,1,1,0,1,1,0,1},
{1,0,1,0,0,0,0,0,2,1},
{1,1,1,1,1,1,1,1,1,1}
};
typedef struct
{
int i; //当前方块的行号
int j; //当前方块的列号
int d; //di是下一可走相邻方位的方位号 1为向右走 2为向下走 3为向左走 4为向上走
} Box;
typedef struct
{
Box base[MaxSize];
int top; //栈顶指针
} SqList;
void MazePath(SqList S)
{
intq=0,m,n,k=0,l; //q 为计数器
m=1;
n=1;
S.top=-1;
do
{
k++;
if(a[m][n]==0)
{
a[m][n]=1;
S.top++;
S.base[S.top].i=m;
S.base[S.top].j=n;
S.base[S.top].d=1;
n=n+1;
q=q+1;
continue;
}
if(a[m][n]==1)
{
if(S.base[S.top].d==1)
{
n=n-1;
S.base[S.top].d=2;
m=m+1;
continue;
}
if(S.base[S.top].d==2)
{
m=m-1;
S.base[S.top].d=3;
n=n-1;
continue;
}
if(S.base[S.top].d==3)
{
n=n+1;
S.base[S.top].d=4;
continue;
}
if(S.base[S.top].d==4)
{
S.top--;
q--;
continue;
}
}
if(a[m][n]==2)
{
printf(" 步数为:%d\n",q);
break;
}
}while(S.top>-1);
printf("执行了%d次循环\n",k);
}
int main()
{
void MazePath(SqList S);
SqList S;
MazePath(S);
return 0;
}